package com.forsir.android.mhfinance.helper;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;

import com.forsir.android.mhfinance.R;
import com.forsir.android.mhfinance.storeObjects.Category;
import com.forsir.android.mhfinance.storeObjects.CategoryList;
import com.forsir.android.mhfinance.storeObjects.Subcategory;

public abstract class ManageExpandableItemActivity extends ManageItemsActivity {
	protected CategoryList mCategoryList = new CategoryList();

	public long parentItemId;
	protected int lastMenuChildPos = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (savedInstanceState != null) {
			lastMenuChildPos = savedInstanceState.getInt("lastMenuChildPos", -1);
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {

		Category category = new Category();
		Subcategory subcategory = new Subcategory();

		final ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();

		final int type = ExpandableListView.getPackedPositionType(info.packedPosition);
		if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
			final int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
			final int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
			lastMenuChildPos = groupPos;
			subcategory = CategoryList.getSubcategory(groupPos, childPos);
			category = CategoryList.getCategory(groupPos);
		} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
			final int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
			lastMenuChildPos = -1;
			category = CategoryList.getCategory(groupPos);
		}

		parentItemId = category.getId();

		switch (item.getItemId()) {
			case R.id.menu_category_new:
				ShowAddNewDialog(category);
				break;
			case R.id.menu_category_delete:
				ShowDeleteDialog(category);
				break;
			case R.id.menu_category_edit:
				ShowEditDialog(category);
				break;
			case R.id.menu_category_new_subcategory:
			case R.id.menu_subcategory_new:
				ShowAddNewSubcategoryDialog();
				break;
			case R.id.menu_subcategory_delete:
				ShowDeleteDialog(subcategory);
				break;
			case R.id.menu_subcategory_edit:
				ShowEditDialog(subcategory);
				break;
		}
		return true;
	}

	public void addNewSubCategory() {
		ShowAddNewSubcategoryDialog();
	}

	public void ShowAddNewSubcategoryDialog() {
		final int title = R.string.menu_subcategory_new;

		final LayoutInflater factory = LayoutInflater.from(this);
		final View textEntryView = factory.inflate(R.layout.dialog, null);

		final EditText text = (EditText) textEntryView.findViewById(R.id.dialog_edit_text);
		text.setHint(R.string.dialog_enter_new_name);
		final TextView viewText = (TextView) textEntryView.findViewById(R.id.dialog_text_view);
		viewText.setText(R.string.dialog_enter_name);

		final Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setView(textEntryView);
		builder.setPositiveButton(R.string.save, addNewItemClickListener);
		builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
			}
		});
		mTextDialog = builder.show();
	}

	private final OnClickListener addNewItemClickListener = new OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			final EditText text = (EditText) mTextDialog.findViewById(R.id.dialog_edit_text);
			final String returnText = text.getText().toString();

			if (returnText.length() != 0) {
				processAddNewItem(returnText, parentItemId);
			}
		}
	};

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("lastMenuChildPos", lastMenuChildPos);
	}

	// must be defined in child
	protected abstract void processAddNewItem(String name, long parentId);

}
